package gob.inei.enaho.view.adapters;

import gob.inei.enaho.context.AppContext;
import gob.inei.enaho.context.Preferencias;
import gob.inei.enaho.controller.activity.EnahoFragmentActivity;
import gob.inei.enaho.model.dao.mapping.TablaHogar;
import gob.inei.enaho.model.domain.enaho.cuestionario.CuestionarioEnaho;
import gob.inei.enaho.model.domain.enaho.cuestionario.Enaho01;
import gob.inei.enaho.model.domain.persona.Hogar;
import gob.inei.enaho.model.service.HogarService;
import gob.inei.enaho2014.R;

import java.util.Date;
import java.util.List;

import util.date.DateUtil;
import util.form.Seccion;
import util.android.AndroidUtil;
import util.android.ViewUtil;
import util.android.adapters.ItemSpinnerAdapter;
import util.android.form.respuesta.deprecated.SpinnerInput;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;

public class CuestionariosDialog 
extends Dialog
{
	private final Hogar hg;
	private final HogarService hogarService = AppContext.getServices().getHogarService(); 

	public CuestionariosDialog(Context context, Hogar hogar) {
		super(context);
		hg = hogar;
	}
	
	@Override
	public void show() {
		//AndroidUtil.mostrarToast(getContext(), "H="+hg.getKey());
		
		final String c = hg.getVivienda().getConglomerado().getCodigo();
		final String v = hg.getVivienda().getCodigo();
		final String h = hg.getCodigo()==null? null : hg.getCodigo()+"";
		
		StringBuilder sb = new StringBuilder();
		sb.append("Conglomerado "+c);
		sb.append(" - Vivienda sel. "+v);
		if(h!=null)
			sb.append(" - Hogar "+h);
		
		final Dialog dialog = this;
		dialog.setContentView(R.layout.dialog_cuestionarios);
		dialog.setTitle(sb.toString());
		
		Date d = new Date();
		ViewUtil.setTextView(dialog, R.id.tvFecha, DateUtil.DateToString(d,DateUtil.dateFormatFecha) );
		ViewUtil.setTextView(dialog, R.id.tvHora, DateUtil.DateToString(d,DateUtil.dateFormatHora) );
		
		Button b;
		
		b = (Button)dialog.findViewById(R.id.btnAperturaVivienda);
		b.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View view) {				
																
				try {
					EnahoFragmentActivity.abrirCapitulo(getContext(),c, v, null, Enaho01.APERTURA_VIVIENDA.getSeccion(), null);
					AppContext.getServices().getViviendaService().aperturarVivienda(c,v);
				} catch (Exception e) {
					AndroidUtil.mostrarExcepcionEnToast(getContext(), e);
				}
										
			}
		});
		
		b = (Button)dialog.findViewById(R.id.btnAperturaHogar);
		b.setOnClickListener(new View.OnClickListener() {		
			@Override
			public void onClick(View view) {		
				
				try {
					//EnahoFragmentActivity.APERTURA_HOGAR
					EnahoFragmentActivity.abrirCapitulo(getContext(),c,v,h,Enaho01.APERTURA_HOGAR.getSeccion(),null);
				} catch (Exception e) {
					AndroidUtil.mostrarExcepcionEnToast(getContext(), e);
				}				
			}
		});
		
		LinearLayout lyCuestionarios = (LinearLayout) dialog.findViewById(R.id.lyDatosHogar);
		if(h==null) {
			lyCuestionarios.setVisibility(View.GONE);
		}
		else {
			//b.setVisibility(View.GONE);
			
			LinearLayout lyEnahos = (LinearLayout) dialog.findViewById(R.id.lyEnahos);
			
			try {
				String val = AppContext.getServices().getHogarService().findValorByColumna(hg, TablaHogar.COMPLETO);
				if(!(val!=null && val.equals("1"))) {
					lyEnahos.setVisibility(View.GONE);
				}
				else {
					
					setCuestionario(dialog,c,v,h,R.id.lyEnaho01,CuestionarioEnaho.ENAHO_01);
					
					/*
					setCuestionario(dialog,c,v,h,CuestionarioEnaho.ENAHO_01A, R.id.lyEnaho01A,EnahoFragmentActivity.CAP100);
					setCuestionario(dialog,c,v,h,CuestionarioEnaho.ENAHO_01B, R.id.lyEnaho01B,EnahoFragmentActivity.CAP100);
					setCuestionario(dialog,c,v,h,CuestionarioEnaho.ENAHO_02, R.id.lyEnaho02,EnahoFragmentActivity.CAP100);
					setCuestionario(dialog,c,v,h,CuestionarioEnaho.ENAHO_04, R.id.lyEnaho04,EnahoFragmentActivity.CAP100);
					*/
				}
				
			} catch (Exception e) {
				AndroidUtil.mostrarExcepcionEnToast(getContext(), e);
			}			
		}
		
		
		
 		super.show();
	}
	
	
	
	
	private void setCuestionario(Dialog dialog, 
			final String c, 
			final String v, 
			final String h,
			int layoutCuestionarioID,
			final CuestionarioEnaho cuest
			) throws Exception {
		
		View ly = dialog.findViewById(layoutCuestionarioID);
		
		Button b = (Button)ly.findViewWithTag("boton");
		
		Spinner spn = (Spinner)ly.findViewWithTag("spinner");
		final SpinnerInput in = new SpinnerInput(spn);
		List<Seccion<CuestionarioEnaho>> lista = hogarService.findCapitulosParaDialogByCuestionario(hg,cuest);
		
		in.setAdapter(new ItemSpinnerAdapter<Seccion<CuestionarioEnaho>>(spn.getContext(),lista) {

			@Override
			public String getKey(Seccion<CuestionarioEnaho> obj) {
				return obj.getEnumName();
			}

			@Override
			public String getLabel(Seccion<CuestionarioEnaho> obj) {
				return obj.getCodigo()+". "+obj.getNombre();
			}			
		});
		
		SharedPreferences sp = Preferencias.leerPreferencias(getContext());
		String secName = sp.getString(cuest.name(), null);
		in.setValor(secName);
		
		b.setOnClickListener(new View.OnClickListener() {		
			@Override
			public void onClick(View view) {				
				try {
					@SuppressWarnings("unchecked")
					Seccion<CuestionarioEnaho> s = (Seccion<CuestionarioEnaho>) in.getItemSeleccionado().getObjeto();
					if(s!=null)
						EnahoFragmentActivity.abrirCapitulo(getContext(),c, v, h, s, null);
				} catch (Exception e) {
					AndroidUtil.mostrarExcepcionEnToast(getContext(), e);
				}
			}
		});
		
	}
	
	public Hogar getHogar() {
		return hg;
	}
}
